<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='菜单',active='indexMenu'">
<header th:replace="admin/header::headerFragment(${title},${active})"></header>

<body class="fixed-left">
<div id="wrapper">
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">博客菜单管理，默认菜单不可被删除！</h4>
                    </div>
                    <div class="col-md-12">
                    	<div class="pull-right">
                            <a href="javascript:openAdd()" class="btn btn-success waves-effect waves-light m-b-5">添加</a>
                        </div>
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>菜单名称</th>
                                <th>地址</th>
                                <th>类型</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="menu : ${menus}">
                                <tr th:attr="id=${menu.id}">
                                    <td th:text="${menu.index}"></td>
                                    <td th:text="${menu.name}"></td>
                                    <td th:text="${menu.url}"></td>
                                    <td th:text="${menu.type=='DEFAULT_MENU'?'链接':(menu.type=='CATEGORY_MENU'?'文章分类':'登录操作')}"></td>
                                    <td>
                                    	<a href="javascript:void(0)" th:onclick="'up('+${menu.id}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-arrow-up"></i></a>
                                        <a href="javascript:void(0)" th:onclick="'down('+${menu.id}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-arrow-down"></i></a>
                                        <a th:if="${menu.name!='导航' && menu.name!='首页'}" href="javascript:void(0)" th:onclick="'del('+${menu.id}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-trash-o"></i> <span>删除</span></a>
                                    </td>
                                </tr>
                            </th:block>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="admin/footer :: footer"></div>
<script src="//cdn.bootcss.com/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<script src="//cdn.bootcss.com/jquery-validate/1.15.1/localization/messages_zh.min.js"></script>
<script type="text/javascript" th:src="@{/admin/plugins/layer/layer.js}"></script>

<script type="text/html" id="addFormDiv">
<form id="addForm" th:action="@{/admin/indexMenu/save}" method="POST" style="width:500px">
	<div style="margin-right:10px; margin-left:10px; margin-top:5px; display:flex;margin-bottom: 0;vertical-align: middle;">
		<label style="width: 70px;padding-top: 8px;">名称</label>
		<input class="form-control valid" type="text" id="name" name="name" placeholder="请输入菜单名称" required="required"/>
	</div>
	<div style="margin-right:10px; margin-left:10px; margin-top:5px; display:flex;margin-bottom: 0;vertical-align: middle;">
		<label style="width: 70px;padding-top: 8px;">链接</label>
		<input class="form-control valid" type="text" id="url" name="url" placeholder="请输入菜单链接" required="required"/>
	</div>
	<div style="margin-right:10px; margin-left:10px; margin-top:5px; display:flex;margin-bottom: 0;vertical-align: middle;">
		<label style="width: 70px;padding-top: 8px;">排序序号</label>
		<input class="form-control valid" type="number" id="index" name="index" placeholder="请输入排序序号" required="required"/>
	</div>
	<div style="margin-right:10px; margin-left:10px; margin-top:5px; display:flex;margin-bottom: 0;vertical-align: middle;">
		<label style="width: 70px;padding-top: 8px;">菜单类型</label>
		<select class="form-control valid" id="type" name="type" required="required">
			<option value="DEFAULT_MENU">普通链接</option>
			<option value="CATEGORY_MENU">文章分类</option>
			<option value="OPTION_MENU">登录操作</option>
		</select>
	</div>
	<br/>
	<button style="margin-left:435px" type="button" class="btn btn-success" onclick="submitAdd()">保存</button>
</form>
</script>

<link rel="stylesheet" type="text/css" href="http://layer.layui.com/demo.css"/>
<script type="text/javascript">
    /*<![CDATA[*/
    	
   	var tale = new $.tale();
    
   	function up(id) {
        tale.alertConfirm({
            title: '确定上升吗?',
            then: function () {
                tale.post({
                    url: ctxPath+'admin/indexMenu/up',
                    data: {id: id},
                    success: function (result) {
                        if (result && result.success) {
                            tale.alertOkAndReload('成功');
                        } else {
                            tale.alertError(result.msg || '失败');
                        }
                    }
                });
            }
        });
    }
   	
   	function down(id) {
        tale.alertConfirm({
            title: '确定下降吗?',
            then: function () {
                tale.post({
                    url: ctxPath+'admin/indexMenu/down',
                    data: {id: id},
                    success: function (result) {
                        if (result && result.success) {
                            tale.alertOkAndReload('成功');
                        } else {
                            tale.alertError(result.msg || '失败');
                        }
                    }
                });
            }
        });
    }
    
    function del(id) {
        tale.alertConfirm({
            title: '确定删除吗?',
            then: function () {
                tale.post({
                    url: ctxPath+'admin/indexMenu/delete',
                    data: {id: id},
                    success: function (result) {
                        if (result && result.success) {
                            tale.alertOkAndReload('删除成功');
                        } else {
                            tale.alertError(result.msg || '删除失败');
                        }
                    }
                });
            }
        });
    }
    
    function openAdd(){
    	
    	layer.open({
    		title: "添加菜单链接",
    		  type: 1,
    		  skin: 'layui-layer-demo', //样式类名
    		  closeBtn: 0, //不显示关闭按钮
    		  anim: 2,
    		  area: ['500px', '270px'],
    		  shadeClose: true, //开启遮罩关闭
    		  content: $("#addFormDiv").html()
    		});
    }

    function submitAdd(){
//     	tale.alertOkAndReload('删除成功');
    	var params = $("#addForm").serialize();
    	layer.closeAll();
    	 tale.post({
             url : ctxPath+'admin/indexMenu/save',
             data: params,
             success: function (result) {
                 if(result && result.success){
                     tale.alertOkAndReload('保存成功');
                 } else {
                     tale.alertError(result.msg || '保存失败');
                 }
             }
         });
    }
    /*]]>*/
</script>
</body>
</html>